package labox.innovation.gameserver.model.zone.form;

import labox.innovation.gameserver.model.zone.L2ZoneForm;

/**
 * 圆形地带，一个标准的圆形地带形式
 *
 *
 * @author  durgus
 */
public class ZoneCylinder extends L2ZoneForm {
	
	private int _x, _y, _rad, _radS;

	public ZoneCylinder(int x, int y, int rad) {
		_x = x;
		_y = y;
		_rad = rad;
		_radS = rad * rad;
	}

	@Override
	public boolean isInsideZone(int x, int y) {
		if ((Math.pow(_x - x, 2) + Math.pow(_y - y, 2)) > _radS)
			return false;
		return true;
	}

	@Override
	public boolean intersectsRectangle(int ax1, int ax2, int ay1, int ay2) {
		// Circles point inside the rectangle?
		if (_x > ax1 && _x < ax2 && _y > ay1 && _y < ay2)
			return true;

		// Any point of the rectangle intersecting the Circle?
		if ((Math.pow(ax1 - _x, 2) + Math.pow(ay1 - _y, 2)) < _radS)
			return true;
		if ((Math.pow(ax1 - _x, 2) + Math.pow(ay2 - _y, 2)) < _radS)
			return true;
		if ((Math.pow(ax2 - _x, 2) + Math.pow(ay1 - _y, 2)) < _radS)
			return true;
		if ((Math.pow(ax2 - _x, 2) + Math.pow(ay2 - _y, 2)) < _radS)
			return true;

		// Collision on any side of the rectangle?
		if (_x > ax1 && _x < ax2) {
			if (Math.abs(_y - ay2) < _rad)
				return true;
			if (Math.abs(_y - ay1) < _rad)
				return true;
		}
		if (_y > ay1 && _y < ay2) {
			if (Math.abs(_x - ax2) < _rad)
				return true;
			if (Math.abs(_x - ax1) < _rad)
				return true;
		}

		return false;
	}

	@Override
	public double getDistanceToZone(int x, int y) {
		return (Math.sqrt((Math.pow(_x - x, 2) + Math.pow(_y - y, 2))) - _rad);
	}
}
